package lg.minecraft.plugin.pokemonmenu.config;

import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.cobblemoncore.util.PokemonPlaceholderReplacer;
import lg.minecraft.plugin.cobblemoncore.util.PokemonUtil;
import lg.minecraft.plugin.library.config.LGBaseConfig;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lombok.Getter;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.chat.hover.content.Content;
import net.md_5.bungee.api.chat.hover.content.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class Message extends LGBaseConfig {

    private String modelToPokemon;
    private String displayedPokemonExpired;
    private String noPermission;
    private String inputNickname;
    private String inputMove;
    private String noMove;
    private String inputPokemon;
    private String noPokemon;
    private String displayCooldown;
    private String commandReloadConfig;
    private String guiTitleParty;
    private String guiTitlePc;
    private String guiTitlePokemon;
    private String guiTitleViewParty;
    private String guiTitleViewPc;
    private String guiTitleViewPokemon;
    private String guiTitleEdit;
    private String guiTitleEditForm;
    private String guiTitleEditMove;
    private String guiTitleEditNature;
    private String guiTitleEditPokeBall;
    private String guiTitlePokemonToItem;
    private String guiTitleItemToPokemon;
    private String healPokemon;
    private String noEmptySlot;
    private String notTradeable;
    private String notMaxIvs;
    private String notEnoughMoney;
    private String guiTitleSearch;
    private String guiTitleSort;

    public Message() {
        super(LGPokemonMenu.getInstance(), "message");
        init();
    }

    @Override
    public void load() {
        super.load();

        modelToPokemon = getString("modelToPokemon");
        displayedPokemonExpired = getString("displayedPokemonExpired");
        noPermission = getString("noPermission");
        inputNickname = getString("input_nickname");
        inputMove = getString("input_move");
        noMove = getString("no_move");
        inputPokemon = getString("input_pokemon");
        noPokemon = getString("no_pokemon");
        displayCooldown = getString("display_cooldown");
        commandReloadConfig = getString("command_reload_config");
        healPokemon = getString("heal_pokemon");
        noEmptySlot = getString("no_empty_slot");
        notTradeable = getString("not_tradeable");
        notMaxIvs = getString("not_max_ivs");
        notEnoughMoney = getString("not_enough_money");

        guiTitleParty = getString("gui_title.party");
        guiTitlePc = getString("gui_title.pc");
        guiTitlePokemon = getString("gui_title.pokemon");
        guiTitleViewParty = getString("gui_title.view_party");
        guiTitleViewPc = getString("gui_title.view_pc");
        guiTitleViewPokemon = getString("gui_title.view_pokemon");
        guiTitleEdit = getString("gui_title.edit");
        guiTitleEditForm = getString("gui_title.edit_form");
        guiTitleEditMove = getString("gui_title.edit_move");
        guiTitleEditNature = getString("gui_title.edit_nature");
        guiTitleEditPokeBall = getString("gui_title.edit_pokeball");
        guiTitlePokemonToItem = getString("gui_title.pokemon_to_item");
        guiTitleItemToPokemon = getString("gui_title.item_to_pokemon");
        guiTitleSearch = getString("gui_title.search");
        guiTitleSort = getString("gui_title.sort");

        PokemonUtil.yes = getString("true");
        PokemonUtil.no = getString("false");
        PokemonUtil.none = getString("none");

        PokemonUtil.NatureTypeFormat = getString("nature_type.format");
        PokemonUtil.ATTACK_UP = getString("nature_type.attack_up");
        PokemonUtil.SPECIAL_ATTACK_UP = getString("nature_type.special_attack_up");
        PokemonUtil.SPECIAL_DEFENSE_UP = getString("nature_type.special_defense_up");
        PokemonUtil.DEFENSE_UP = getString("nature_type.defense_up");
        PokemonUtil.SPEED_UP = getString("nature_type.speed_up");
        PokemonUtil.ATTACK_DOWN = getString("nature_type.attack_down");
        PokemonUtil.SPECIAL_ATTACK_DOWN = getString("nature_type.special_attack_down");
        PokemonUtil.SPECIAL_DEFENSE_DOWN = getString("nature_type.special_defense_down");
        PokemonUtil.DEFENSE_DOWN = getString("nature_type.defense_down");
        PokemonUtil.SPEED_DOWN = getString("nature_type.speed_down");
        PokemonUtil.BALANCED = getString("nature_type.balanced");
    }

    /**
     * 构建单个宝可梦展示的文本组件
     *
     * @param playerName 展示者名字
     * @param timestamp 时间戳
     * @param pokemon 宝可梦对象
     * @return TextComponent
     */
    public TextComponent getShowPokemon(String playerName, long timestamp, Pokemon pokemon) {
        Map<String, String> placeholders = PokemonPlaceholderReplacer.buildPokemonInfoPlaceholders(pokemon);
        placeholders.put("{player}", playerName);
        return buildComponent("show.pokemon", timestamp, placeholders);
    }

    /**
     * 构建队伍展示的文本组件
     *
     * @param playerName 展示者名字
     * @param timestamp 时间戳
     * @return TextComponent
     */
    public TextComponent getShowParty(String playerName, long timestamp) {
        Map<String, String> placeholders = new HashMap<>();
        placeholders.put("{player}", playerName);
        return buildComponent("show.party", timestamp, placeholders);
    }

    /**
     * 构建 PC 展示的文本组件
     *
     * @param playerName 展示者名字
     * @param timestamp 时间戳
     * @return TextComponent
     */
    public TextComponent getShowPc(String playerName, long timestamp) {
        Map<String, String> placeholders = new HashMap<>();
        placeholders.put("{player}", playerName);
        return buildComponent("show.pc", timestamp, placeholders);
    }

    /**
     * 内部通用构建方法
     */
    private TextComponent buildComponent(String prefix, long timestamp, Map<String, String> placeholders) {
        String textKey = prefix + ".text";
        String nameKey = prefix + ".name";
        String hoverKey = prefix + ".hover";

        String rawText = getString(textKey, "");
        String rawName = getString(nameKey, "%name%");
        List<String> rawHover = getStringList(hoverKey);

        // 替换变量
        String replacedText = PokemonPlaceholderReplacer.replacePlaceholders(rawText, placeholders);
        String replacedName = PokemonPlaceholderReplacer.replacePlaceholders(rawName, placeholders);
        List<String> replacedHover = PokemonPlaceholderReplacer.replacePlaceholders(rawHover, placeholders);

        // 构建组件
        String[] parts = replacedText.split("%name%", 2);
        TextComponent mainComponent = new TextComponent(parts[0]);
        TextComponent nameComponent = new TextComponent(replacedName);
        TextComponent suffixComponent = new TextComponent(parts.length > 1 ? parts[1] : "");

        // 设置悬停内容
        List<Content> contents = new ArrayList<>();
        for (int i = 0; i < replacedHover.size(); i++) {
            String line = replacedHover.get(i);
            if (i + 1 == replacedHover.size()) {
                 contents.add(new Text(line));
            } else  {
                contents.add(new Text(line + "\n"));
            }
        }

        // 设置点击事件
        nameComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, contents));
        nameComponent.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/pm view " + timestamp));

        mainComponent.addExtra(nameComponent);
        mainComponent.addExtra(suffixComponent);

        return mainComponent;
    }

    public String getDisplayCooldown(int time) {
        return displayCooldown.replace("{time}", String.valueOf(time));
    }

}
